/**
 * 
 */
package de.cbf.cam.dsa.battle.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.part.ViewPart;

import de.cbf.cam.dsa.battle.Activator;
import de.cbf.cam.dsa.battle.ui.composites.CompositeNPCDisplay;
import de.cbf.cam.dsa.model.entities.BattleChar;
import de.cbf.cam.dsa.model.entities.FightPosition;


/**
 * @author Nebelritter
 *
 */
public class AttackerView extends ViewPart implements IPartListener2{
	public static final String ID="de.cbf.cam.dsa.battle.views.AttackerView";
	
	/**
	 * @uml.property  name="attackBT"
	 * @uml.associationEnd  
	 */
	private Button attackBT;
	/**
	 * @uml.property  name="changeEnemyBT"
	 * @uml.associationEnd  
	 */
	private Button changeEnemyBT;
	/**
	 * @uml.property  name="changeMainWeaponBT"
	 * @uml.associationEnd  
	 */
	private Button changeMainWeaponBT;
	/**
	 * @uml.property  name="changeSecondWeaponBT"
	 * @uml.associationEnd  
	 */
	private Button changeSecondWeaponBT;
	/**
	 * @uml.property  name="changeWeaponTalentBT"
	 * @uml.associationEnd  
	 */
	private Button changeWeaponTalentBT;
	/**
	 * @uml.property  name="fleeBT"
	 * @uml.associationEnd  
	 */
	private Button fleeBT;
	/**
	 * @uml.property  name="otherActionsBT"
	 * @uml.associationEnd  
	 */
	private Button otherActionsBT;
	/**
	 * @uml.property  name="positionBT"
	 * @uml.associationEnd  
	 */
	private Button positionBT;	
	/**
	 * @uml.property  name="standUpBT"
	 * @uml.associationEnd  
	 */
	private Button standUpBT;
	/**
	 * @uml.property  name="useEquipmentBT"
	 * @uml.associationEnd  
	 */
	private Button useEquipmentBT;	
	
	/**
	 * @uml.property  name="attackListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> attackListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="changeEnemyListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> changeEnemyListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="changeMainWeaponListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> changeMainWeaponListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="changeSecondWeaponListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> changeSecondWeaponListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="changeWeaponTalentListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> changeWeaponTalentListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="fleeListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> fleeListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="otherActionsListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> otherActionsListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="standUpListeners"
	 */
	private List<ActionListener> standUpListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="positionListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> positionListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="useEquipmentListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> useEquipmentListeners = new ArrayList<ActionListener>();	
		
	/**
	 * @uml.property  name="infoPart"
	 * @uml.associationEnd  
	 */
	private CompositeNPCDisplay infoPart;
	
	/**
	 * @uml.property  name="enemysGRP"
	 * @uml.associationEnd  
	 */
	private Group enemysGRP;
	
	
	public AttackerView() {
		Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(this);
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite mainCP= new Composite(parent,SWT.NORMAL);
		mainCP.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		GridLayout mainLO = new GridLayout(1,false);
		mainLO.marginHeight=0;
		mainLO.marginWidth=5;
		mainCP.setLayout(mainLO);
		
			infoPart=new CompositeNPCDisplay(mainCP,new GridData(SWT.FILL,SWT.NORMAL,true,false,1,1));				
					
			Group actionGRP = new Group(mainCP, SWT.NORMAL);		
			actionGRP.setLayoutData(new GridData(SWT.FILL,SWT.NORMAL,true,false,1,1));
			GridLayout actionLO = new GridLayout(3,false);
			actionLO.marginHeight=0;
			actionLO.marginWidth=5;
			actionGRP.setLayout(actionLO);
			actionGRP.setText("Aktionen");
			
				buildActionComponent(actionGRP);				
				
			enemysGRP = new Group(mainCP, SWT.V_SCROLL);		
			enemysGRP.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,20));
			GridLayout tableLO = new GridLayout(1,false);
			tableLO.marginHeight=5;
			tableLO.marginWidth=5;
			enemysGRP.setLayout(tableLO);
			enemysGRP.setText("Derzeitige Gegner");				
	}
	

	@Override
	public void setFocus() {
	}
	@Override
	public void dispose() {
//		Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().removePartListener(this); //throws NPE at Application finish
	}	
	
	public void setCharacter(BattleChar attacker){
		if(attacker!=null){			
			infoPart.setCharacter(attacker);
			buildEnemyComponent(enemysGRP, attacker);
			
			activateButtons(attacker);
		}			
	}
	
	private void activateButtons(BattleChar attacker) {
		disableAllButtons();		
		changeEnemyBT.setEnabled(true);
		for (Iterator<Entry<BattleChar,FightPosition>> iterator = attacker.getEnemysFull().iterator(); iterator.hasNext();) {
			Entry<BattleChar,FightPosition> entry = (Entry<BattleChar,FightPosition>) iterator.next();			
			String dk = entry.getValue().getDk();
			if(dk!=null&&!dk.equals("-")){
				attackBT.setEnabled(true);
			}				
		}	
		useEquipmentBT.setEnabled(true);		
		otherActionsBT.setEnabled(true);
	}
	public void disableAllButtons(){
		changeEnemyBT	.setEnabled(false);
		attackBT		.setEnabled(false);
		useEquipmentBT	.setEnabled(false);
		otherActionsBT	.setEnabled(false);		
		
		standUpBT.setEnabled(false);
		positionBT.setEnabled(false);		
		fleeBT.setEnabled(false);
		changeMainWeaponBT.setEnabled(false);
		changeWeaponTalentBT.setEnabled(false);
		changeSecondWeaponBT.setEnabled(false);		
	}

	private void buildActionComponent(Composite parent) {
		
		//change group		
		Group changeGRP = new Group(parent, SWT.NORMAL);
		changeGRP.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,3));
		GridLayout changeLO =new GridLayout(1, false);
		changeLO.marginHeight=0;
		changeLO.marginWidth=5;		
		changeGRP.setLayout(changeLO);	
		changeGRP.setText("Wechsel");
		
			changeEnemyBT = new Button(changeGRP, SWT.PUSH);
			changeEnemyBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			changeEnemyBT.setText("Gegner...");			
			changeEnemyBT.addListener(SWT.Selection,new ChangeEnemyListener());		
			
			useEquipmentBT = new Button(changeGRP, SWT.PUSH);
			useEquipmentBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			useEquipmentBT.setText("Benutzen...");			
			useEquipmentBT.addListener(SWT.Selection,new UseEquipmentListener());
			
			changeMainWeaponBT = new Button(changeGRP, SWT.PUSH);
			changeMainWeaponBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			changeMainWeaponBT.setText("???");			
			changeMainWeaponBT.addListener(SWT.Selection,new ChangeMainWeaponListener());
			
			
			changeSecondWeaponBT = new Button(changeGRP, SWT.PUSH);
			changeSecondWeaponBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			changeSecondWeaponBT.setText("???");			
			changeSecondWeaponBT.addListener(SWT.Selection,new ChangeSecondWeaponListener());
			
			changeWeaponTalentBT = new Button(changeGRP, SWT.PUSH);
			changeWeaponTalentBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			changeWeaponTalentBT.setText("???");
			changeWeaponTalentBT.addListener(SWT.Selection,new ChangeWeaponTalentListener());
			
			
		
		Group specialActionsGRP = new Group(parent, SWT.NORMAL);
		specialActionsGRP.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,3));
		GridLayout specialActionsLO =new GridLayout(1, false);
		specialActionsLO.marginHeight=0;
		specialActionsLO.marginWidth=5;		
		specialActionsGRP.setLayout(specialActionsLO);	
		specialActionsGRP.setText("Aktionen");
		
			otherActionsBT = new Button(specialActionsGRP, SWT.PUSH);
			otherActionsBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			otherActionsBT.setText("Andere Aktionen");			
			otherActionsBT.addListener(SWT.Selection,new OtherActionsListener());
		
			
			attackBT = new Button(specialActionsGRP, SWT.PUSH);
			attackBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			attackBT.setText("Angriff...");			
			attackBT.addListener(SWT.Selection,new AttackListener());
			
			standUpBT = new Button(specialActionsGRP, SWT.PUSH);
			standUpBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			standUpBT.setText("Zaubern...");
			standUpBT.addListener(SWT.Selection,new StandUpListener());
			
			
			positionBT = new Button(specialActionsGRP, SWT.PUSH);
			positionBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			positionBT.setText("???");	
			positionBT.addListener(SWT.Selection,new PositionListener());
			
			fleeBT = new Button(specialActionsGRP, SWT.PUSH);
			fleeBT.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,1,1));
			fleeBT.setText("???");
			fleeBT.addListener(SWT.Selection,new FleeListener());
		
		disableAllButtons();
	}

	private void buildEnemyComponent(Composite parent,BattleChar character) {
		Control[] children = parent.getChildren();
		for(int i=0;i<children.length;i++){
			children[i].dispose();
		}
		if(character!=null){
			List<BattleChar> enemys =character.getEnemysNames();
			for(int i=0;i<enemys.size();i++){
				CompositeNPCDisplay enemyDisplay = new CompositeNPCDisplay(parent,new GridData(SWT.FILL,SWT.NORMAL,true,false,1,1));	
				enemyDisplay.setCharacter(enemys.get(i));
				enemyDisplay.setTitle(enemys.get(i).getCharacter().getName());
			}
		}		
		parent.layout(true,true);
	}
	
	
//	==============================
//	listener registry
//	==============================
	public boolean registerAttackListener(ActionListener listener){
		return attackListeners.add(listener);		
	}
	public boolean registerChangeEnemyListener(ActionListener listener){
		return changeEnemyListeners.add(listener);		
	}
	public boolean registerChangeMainWeaponListener(ActionListener listener){
		return changeMainWeaponListeners.add(listener);		
	}
	public boolean registerChangeSecondWeaponListener(ActionListener listener){
		return changeSecondWeaponListeners.add(listener);		
	}
	public boolean registerChangeWeaponTalentListener(ActionListener listener){
		return changeWeaponTalentListeners.add(listener);		
	}
	public boolean registerFleeListener(ActionListener listener){
		return fleeListeners.add(listener);		
	}
	public boolean registerOtherActionsListener(ActionListener listener){
		return otherActionsListeners.add(listener);		
	}
	public boolean registerPositionListener(ActionListener listener){
		return positionListeners.add(listener);		
	}
	public boolean registerUseEquipmentListener(ActionListener listener){
		return useEquipmentListeners.add(listener);		
	}	
	
	public boolean deRegisterAttackListener(ActionListener listener){
		return attackListeners.remove(listener);
	}	
	public boolean deRegisterChangeEnemyListener(ActionListener listener){
		return changeEnemyListeners.remove(listener);
	}	
	public boolean deRegisterChangeMainWeaponListener(ActionListener listener){
		return changeMainWeaponListeners.remove(listener);
	}	
	public boolean deRegisterChangeSecondWeaponListener(ActionListener listener){
		return changeSecondWeaponListeners.remove(listener);
	}	
	public boolean deRegisterChangeWeaponTalentListener(ActionListener listener){
		return changeWeaponTalentListeners.remove(listener);
	}	
	public boolean deRegisterFleeListener(ActionListener listener){
		return fleeListeners.remove(listener);
	}	
	public boolean deRegisterOtherActionsListener(ActionListener listener){
		return otherActionsListeners.remove(listener);
	}	
	public boolean deRegisterPositionListener(ActionListener listener){
		return positionListeners.remove(listener);
	}	
	public boolean deRegisterUseEquipmentListener(ActionListener listener){
		return useEquipmentListeners.remove(listener);
	}
//	==============================
//	button advancer
//	==============================
	private class ChangeEnemyListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"changeEnemy");
			for (ActionListener actionListerner : changeEnemyListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class AttackListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"attack");
			for (ActionListener actionListerner : attackListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class OtherActionsListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"orient");
			for (ActionListener actionListerner : otherActionsListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class StandUpListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"standUp");
			for (ActionListener actionListerner : standUpListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class PositionListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"zone");
			for (ActionListener actionListerner : positionListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class UseEquipmentListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"useEquipment");
			for (ActionListener actionListerner : useEquipmentListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}
		}		
	}
	private class FleeListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"flee");
			for (ActionListener actionListerner : fleeListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}			
		}		
	}
	private class ChangeMainWeaponListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"changeMainWeapon");
			for (ActionListener actionListerner : changeMainWeaponListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}			
		}		
	}
	private class ChangeSecondWeaponListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"changeSecondWeapon");
			for (ActionListener actionListerner : changeSecondWeaponListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}			
		}		
	}
	private class ChangeWeaponTalentListener implements Listener{		
		public void handleEvent(Event event) {
			ActionEvent actionEvent = new ActionEvent(AttackerView.this, ActionEvent.ACTION_PERFORMED,"changeSecondWeapon");
			for (ActionListener actionListerner : changeWeaponTalentListeners) {				
				actionListerner.actionPerformed(actionEvent);
			}			
		}		
	}
	@Override
	public void partActivated(IWorkbenchPartReference partRef) {		
	}
	@Override
	public void partBroughtToTop(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partClosed(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partDeactivated(IWorkbenchPartReference partRef) {			
	}
	@Override
	public void partHidden(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partInputChanged(IWorkbenchPartReference partRef) {
	}
	@Override
	public void partOpened(IWorkbenchPartReference partRef) {
		if(partRef.getId().equals(ID)){
			Activator.getDefault().setAttackerView(this);
		}	
	}
	@Override
	public void partVisible(IWorkbenchPartReference partRef) {
	}

	
	
}
